package com.sx.utils;

import com.sx.dao.OptionRecordDao;
import com.sx.dao.daoImpl.OptionRecordDaoImpl;

import java.text.SimpleDateFormat;
import java.util.Date;

//生成操作记录id
public class GetOptionRecordID {
    private static final OptionRecordDao OptionRecordDao = new OptionRecordDaoImpl();
    public static String generateOperationRecordId() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String datePart = dateFormat.format(new Date());
        String sequencePart = "0000";

        // 从数据库中读取最大的操作记录 id
        String maxOperationRecordId = OptionRecordDao.getMaxOperationRecordId();
        if (maxOperationRecordId!= null &&!maxOperationRecordId.isEmpty()) {
            // 提取序列号部分
            sequencePart = maxOperationRecordId.substring(maxOperationRecordId.length() - 4);
        }

        int sequence = Integer.parseInt(sequencePart);
        sequence++;
        // 确保序列号部分是 4 位数字，不足 4 位的在前面补 0
        String newSequencePart = String.format("%04d", sequence);

        return datePart + newSequencePart;
    }
}
